using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.IO;

namespace YF.Art
{
    public class DTBakePrefabs
    {

        // [MenuItem("Assets/复制对象/复制", false)]
        public static void work()
        {
            Object go = Selection.activeObject;
            if (go == null) return;
            ChangeGUIDwork(go);
        }
        // [MenuItem("Assets/复制对象/N复制", false)]
        // public static void workC()
        // {
        //     Object go = Selection.activeObject;
        //     if (go == null) return;
        //     ChangeGUIDwork(go, true);
        // }


        [MenuItem("Assets/DT 辅助工具[dtc]/复制对象[多选]", false, 1)]
        public static void works()
        {
            string[] go = Selection.assetGUIDs;
            foreach (string g in go)
            {
                Object obj = AssetDatabase.LoadAssetAtPath<Object>(AssetDatabase.GUIDToAssetPath(g));
                if (obj == null) continue;
                ChangeGUIDwork(obj, true);
            }
        }


        static void ChangeGUIDwork(Object go, bool isCopyCommon = false)
        {
            List<ObjectData> data = new List<ObjectData>();
            data.Clear();
            string basePath;
            string newBasePath;
            basePath = Path.GetDirectoryName(AssetDatabase.GetAssetPath(go));
            newBasePath = basePath + "\\" + "New " + Path.GetFileName(basePath);
            if (!Directory.Exists(newBasePath))
            {
                AssetDatabase.CreateFolder(basePath, "New " + Path.GetFileName(basePath));
                AssetDatabase.Refresh();
            }

            var newtmpobj = newBasePath + "\\" + Path.GetFileName(AssetDatabase.GetAssetPath(go));
            var jjj = AssetDatabase.LoadAssetAtPath<Object>(newtmpobj);
            if (jjj != null)
            {
                AssetDatabase.DeleteAsset(newtmpobj);
            }

            //获取对象数组
            string[] objects = AssetDatabase.GetDependencies(AssetDatabase.GetAssetPath(go));

            //按地址把这些对象复制到新目录
            foreach (var g in objects)
            {

                ObjectData d = new ObjectData();
                d.oldGUID = AssetDatabase.AssetPathToGUID(g);
                if (IsCommon(g, isCopyCommon)) continue;

                string n = Path.GetFileName(g);
                var aaa = AssetDatabase.LoadAssetAtPath<Object>(g);
                string np = newBasePath + "/" + n;
                string t = aaa.GetType().ToString();
                switch (t)
                {
                    case "UnityEditor.MonoScript":
                        break;
                    case "UnityEngine.Texture2D":
                        np = newBasePath + "/Textures/" + n;
                        creatNewFolder(newBasePath, "Textures");
                        break;
                    case "UnityEngine.Material":
                        np = newBasePath + "/Materials/" + n;
                        creatNewFolder(newBasePath, "Materials");
                        break;
                    case "UnityEditor.Animations.AnimatorController":
                        np = newBasePath + "/Animations/" + n;
                        creatNewFolder(newBasePath, "Animations");
                        break;
                    case "UnityEngine.AnimatorOverrideController":
                        np = newBasePath + "/Animations/" + n;
                        creatNewFolder(newBasePath, "Animations");
                        break;
                    case "UnityEngine.AnimationClip":
                        np = newBasePath + "/Animations/" + n;
                        creatNewFolder(newBasePath, "Animations");
                        break;
                    case "UnityEngine.GameObject":   //Models
                        string exname = Path.GetExtension(g).ToLower();
                        if (exname == ".fbx" || exname == ".obj")
                        {
                            creatNewFolder(newBasePath, "Models");
                            np = newBasePath + "/Models/" + n;
                        }
                        else
                        {
                            np = newBasePath + "/" + n;
                        }
                        break;
                    case "UnityEngine.Mesh":
                        creatNewFolder(newBasePath, "Models");
                        np = newBasePath + "/Models/" + n;
                        break;

                    default:
                        np = newBasePath + "/" + n;
                        break;
                }
                np = np.Replace("\\", "/");
                var tmpobj = AssetDatabase.LoadAssetAtPath(np, typeof(UnityEngine.Object));
                if (tmpobj != null)
                {
                    d.newGUID = AssetDatabase.AssetPathToGUID(np);
                }
                else
                {
                    if (AssetDatabase.CopyAsset(g, np))
                    {
                        d.newGUID = AssetDatabase.AssetPathToGUID(np);
                    }
                }
                data.Add(d);
            }

            foreach (var t in data)
            {
                string fp = AssetDatabase.GUIDToAssetPath(t.newGUID);
                string tm = Path.GetExtension(fp).ToLower();
                if (tm == ".fbx" || tm == ".png" || tm == ".jpg" || tm == ".tga")
                    continue;
                if (tm == ".shader")
                    continue;
                if (tm == ".cs")
                    continue;
                filework(fp, data);
            }
            AssetDatabase.Refresh();
        }

        static bool IsCommon(string path, bool isCopyCommon = false)
        {
            if (path.ToLower().IndexOf("common") >= 0)
            {
                Debug.LogWarning("这个 " + path + " 对象存在于Common目录下,被忽略!");
            }
            bool a = path.ToLower().IndexOf("common") > 0;

            if (a)
            {
                var g = AssetDatabase.LoadAssetAtPath<Object>(path);
                string t = g.GetType().ToString();
                a = false;
                if (t == "UnityEngine.Texture2D")
                {
                    a = true;
                }
                if (t == "UnityEngine.GameObject")
                {
                    string exname = Path.GetExtension(path).ToLower();
                    if (exname == ".fbx" || exname == ".obj")
                    {
                        a = true;
                    }
                }
            }
            bool b = Path.GetExtension(path).ToLower() == ".shader";

            bool c = Path.GetExtension(path).ToLower() == ".cs";
            // return path.ToLower().IndexOf("common") > 0;
            return a || b || c;
        }
        static void creatNewFolder(string basePath, string pathname)
        {
            string path = basePath + "/" + pathname;
            path = path.Replace("/", "\\");
            if (!Directory.Exists(path))
            {
                AssetDatabase.CreateFolder(basePath, pathname);
                AssetDatabase.Refresh();
            }
        }
        static void filework(string fp, List<ObjectData> d)
        {
            string[] filedata = File.ReadAllLines(fp);
            foreach (ObjectData g in d)
            {
                string o = "guid: " + g.oldGUID;
                string n = "guid: " + g.newGUID;
                for (int i = 0; i < filedata.Length; i++)
                {
                    if (filedata[i].IndexOf(o) > -1)
                    {
                        filedata[i] = filedata[i].Replace(o, n);
                    }
                }
            }
            string filetxt = string.Join("\r\n", filedata);
            StreamWriter sw = new StreamWriter(fp);
            sw.Write(filetxt);
            sw.Close();
        }
        public class ObjectData
        {
            public string oldGUID;
            public string newGUID;

        }
    }
}

